package com.dykj.fireFighting.web.base.effecttimesetting;

import java.util.*;

import com.dykj.fireFighting.api.base.effecttimesetting.entity.EffectiveTimeSetting;
import com.dykj.fireFighting.api.base.effecttimesetting.service.IEffectiveTimeSettingService;
import com.dykj.fireFighting.api.base.enterprise.entity.Enterprise;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


import javax.validation.Valid;



/**
 * 有效时间设置接口
 *
 * @Author zly
 * @Date  2021-08-12 14:35:29
 */
@Slf4j
@Api(value = "有效时间设置接口", tags = {"有效时间设置接口"})
@RestController
@RequestMapping("effectiveTimeSetting")
public class EffectiveTimeSettingController {

    @DubboReference
    private IEffectiveTimeSettingService effectiveTimeSettingService;
    @DubboReference
    private IEnterpriseService enterpriseService;

    /**
     * 有效时间设置列表
     *
     * @return
     */
    @AutoLog(value = "有效时间设置-分页列表查询")
    @ApiOperation(value = "有效时间设置分页列表", notes = "有效时间设置分页列表")
    @GetMapping("/list")
    //@RequiresPermissions("effectiveTimeSetting/list")
    public CommonResult<PageVo<EffectiveTimeSetting>> treeGrid(CommonDto commonDto) {
        LoginUserVo user = ShiroUtil.getUser();
        Enterprise enterprise = enterpriseService.getInfoByBindingCode(user.getBindingCode());
        HashMap<String, Object> params = new HashMap<>(16);
        //社会单位  查询本社会单位关联的监控中心的
        if(0 == user.getEnterpriseType()){
            params.put("unitId",enterprise.getId().toString());
        }
        if(2 == user.getEnterpriseType()){
            params.put("unitId",user.getEnterpriseId().toString());
        }
        PageVo<EffectiveTimeSetting> page = effectiveTimeSettingService.queryList(commonDto,params);
        return CommonResult.success(page);
    }

    /**
     * 获取有效时间设置信息
     */
    @AutoLog(value = "有效时间设置-获取有效时间设置信息")
    @ApiOperation(value = "获取有效时间设置信息", notes = "获取有效时间设置信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
   // @RequiresPermissions("effectiveTimeSetting/info")
    public CommonResult<EffectiveTimeSetting> info(@PathVariable("id") String id){
        EffectiveTimeSetting effectiveTimeSetting = effectiveTimeSettingService.getById(id);
        return CommonResult.success(effectiveTimeSetting);
    }

    /**
     * 保存有效时间设置信息
     */
    @AutoLog(value = "有效时间设置-保存有效时间设置信息")
    @ApiOperation(value = "保存有效时间设置信息", notes = "保存有效时间设置信息")
    @PostMapping("/save")
    //@RequiresPermissions("effectiveTimeSetting/save")
    public CommonResult save(@Valid @RequestBody EffectiveTimeSetting effectiveTimeSetting) {
        try {
            LoginUserVo loginUserVo = ShiroUtil.getUser();
            Long count=effectiveTimeSettingService.selectByUnintId(loginUserVo.getEnterpriseId().toString());
            if(null != count && count >= 1){
                return CommonResult.failed("该单位已设置有效时间");
            }else{
                effectiveTimeSetting.setCreateTime(new Date());
                effectiveTimeSetting.setUnitId(loginUserVo.getEnterpriseId().toString());
                effectiveTimeSettingService.save(effectiveTimeSetting);
                return CommonResult.success("添加成功");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改有效时间设置信息
     */
    @AutoLog(value = "有效时间设置-修改有效时间设置信息")
    @ApiOperation(value = "修改有效时间设置信息", notes = "修改有效时间设置信息")
    @PostMapping("/update")
    //@RequiresPermissions("effectiveTimeSetting/update")
    public CommonResult update(@Valid @RequestBody EffectiveTimeSetting effectiveTimeSetting) {
        try {

                effectiveTimeSettingService.updateById(effectiveTimeSetting);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除有效时间设置信息
     */
    @AutoLog(value = "有效时间设置-删除有效时间设置信息")
    @ApiOperation(value = "删除有效时间设置信息", notes = "删除有效时间设置信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    //@RequiresPermissions("effectiveTimeSetting/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
            effectiveTimeSettingService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
}
